import { JXDef } from "../../../conventions/JXCommon";
import { GCtrl } from "../../../Core/GCtrl";
import GViewBase from "../../../Core/GView/GViewBase";
import { AudioMgr } from "../../../Core/Manager/AudioMgr";
import { CMsg, HERO_TRAIT, ITEM_DETAIL_FLAG, ITEM_TYPE, PART_ITEM, PointInfo, SQUAD_BUTTON } from "../../Common/Define";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
import JXItem from "../Common/JXItem";
import { JXEHeroBgAniNames } from "../JXBattle/JXULDefine";
import HeroChange from "./HeroChange";


const { ccclass, property, menu } = cc._decorator;

@ccclass @menu('View/Hero/FeatureCtrl')
export default class FeatureCtrl extends GViewBase {
    @property({ type: HeroChange, tooltip: "英雄人物" }) HeroChange: HeroChange = null;
    @property({ type: cc.Label, tooltip: "英雄人物" }) heroTxt: cc.Label = null;
    @property({ type: sp.Skeleton, tooltip: "英雄品质光芒" }) sk_trait: sp.Skeleton = null;
    @property({ type: cc.Node, tooltip: "英雄碎片节点" }) item: cc.Node = null;
    @property({ type: cc.Label, tooltip: "英雄名字" }) heroName: cc.Label = null;
    protected value: number[] = null;
    protected index: number = null;
    onGStart(FEATREWARD: FEATREWARD) {
        this.value = FEATREWARD.reward;
        this.index = FEATREWARD.index;
        this.heroData = this.value[PART_ITEM.ID];
        if (this.value[PART_ITEM.TYPE] == ITEM_TYPE.HERO) {
            this.initHeroSpine();
        } else {
            this.initItem();
        }

    }

    private heroData: number = null;

    /**初始英雄人物 */
    protected initHeroSpine() {
        AudioMgr.Ins().playEffect(Res.audio.addHero);
        let allHero = GameMgr.rHeroData.getHaveHeros(SQUAD_BUTTON.ALL);
        allHero.have.forEach((v) => {
            if (v.tableId == this.heroData) {
                this.HeroChange.init(v);
                let heroInfo = GameMgr.heroData.getRaw<SHeroDataRaw>(this.heroData);
                this.heroTxt.string = heroInfo.show;
                this.initTrait(heroInfo);
                this.sk_trait.setAnimation(0, JXEHeroBgAniNames.white, true)
                this.heroName.string = heroInfo.name;
            }
        })
    }



    /**英雄品质光芒 */
    protected initTrait(heroInfo) {
        switch (heroInfo.trait) {
            case HERO_TRAIT.WHITE:
                {

                    this.sk_trait.setAnimation(0, JXEHeroBgAniNames.white, true)
                    break;
                }
            case HERO_TRAIT.GREEN:
                {
                    this.heroName.node.color = cc.color(47, 242, 89, 255);
                    this.sk_trait.setAnimation(0, JXEHeroBgAniNames.green, true)
                    break;
                }
            case HERO_TRAIT.BLUE:
                {
                    this.heroName.node.color = cc.color(114, 194, 255, 255);
                    this.sk_trait.setAnimation(0, JXEHeroBgAniNames.blue, true)
                    break;
                }
            case HERO_TRAIT.PURPLE:
                {
                    this.heroName.node.color = cc.color(240, 109, 255, 255);
                    this.sk_trait.setAnimation(0, JXEHeroBgAniNames.purple, true)
                    break;
                }
            case HERO_TRAIT.YELLOW:
                {
                    this.heroName.node.color = cc.color(255, 251, 58, 255);
                    this.sk_trait.setAnimation(0, JXEHeroBgAniNames.yellow, true)
                    break;
                }
            case HERO_TRAIT.RED:
                {
                    this.heroName.node.color = cc.color(255, 85, 76, 255);
                    this.sk_trait.setAnimation(0, JXEHeroBgAniNames.red, true)
                    break;
                }

            default:
                break;
        }

    }

    protected doubleClick() {
        GameMgr.sdkMgr.watchAd(() => {
            GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, [this.value]);
            GameMgr.uiMgr.showToast(JXLocales.tip.doubleClick);
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.FeatureCtrl, { msg: PointInfo.FeatureCtrl_ad })
            this.viewClose(null, true);
        })
    }


    protected initItem() {
        this.sk_trait.node.active = false;
        this.heroName.node.parent.active = false;
        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
            let comp = this.assetImpl.addGChild<JXItem>(prefab);
            comp.node.parent = this.item;
            let item = new RJXItem(this.value);
            let heroInfo = GameMgr.itemData.getRaw<SItemDataRaw>(this.heroData);
            this.heroTxt.string = heroInfo.txt;

            comp.setView(item, ITEM_DETAIL_FLAG.BAG | ITEM_DETAIL_FLAG.NUMPOS_ITEM_BC | ITEM_DETAIL_FLAG.NOT_TOUCH | ITEM_DETAIL_FLAG.SHOWLIGHT | ITEM_DETAIL_FLAG.LIGHT);
        })
    }

    viewClose(_, bool: boolean = true) {
        if (bool) {
            let FEAT: FEAT = {
                bool: bool,
                reward: this.value,
                index: this.index,
            }
            GCtrl.ES.emit(CMsg.client.home.onFeature, FEAT)
        }
        this.onClose();
    }



    close() {
        let FEAT: FEAT = {
            bool: false,
            reward: [],
            index: this.index,
        }
        GCtrl.ES.emit(CMsg.client.home.onFeature, FEAT);
        this.onClose();
    }
}
